﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Linq;
using QLKetNoi;

namespace QLPhanQuyen.DAO
{
    /// <summary>
    /// thực hiện các thao tác trên table NGUOIDUNG với LinQ
    /// </summary>
    class NguoiDungDAO
    {
        QLDNDataContext db = new QLDNDataContext();
        Table<NGUOIDUNG> NguoiDungs;

        VaiTroDAO VTDAO = new VaiTroDAO();

        /// <summary>
        /// Thêm mới người dùng cào data
        /// </summary>
        public bool ThemNguoiDung(NGUOIDUNG ND)
        {
            NGUOIDUNG nd = new NGUOIDUNG();
            nd.MaNguoiDung = ND.MaNguoiDung;
            nd.TenNguoiDung = ND.TenNguoiDung;
            nd.TaiKhoan = ND.TaiKhoan;
            nd.MatKhau = ND.MatKhau;
            nd.MaVaiTro = ND.MaVaiTro;
            nd.NhomNguoiDung = ND.NhomNguoiDung;
            nd.DienGiai = ND.DienGiai;
            nd.TrangThai = ND.TrangThai;
            try
            {
                NguoiDungs.InsertOnSubmit(nd);
                db.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Lấy mã người dùng cao nhất 
        /// </summary>
        /// <returns></returns>
        public string MaCaoNhat()
        {
            NguoiDungs = db.GetTable<NGUOIDUNG>();

            var query = (from nd in NguoiDungs.AsEnumerable()
                         select nd.MaNguoiDung).Max();
            return query;
        }
        /// <summary>
        /// Hàm kiểm tra tài khoản có tồn tại hay chưa
        /// </summary>
        /// <param name="taiKhoan"></param>
        /// <returns></returns>
        public bool KiemTraTonTai(string taiKhoan)
        {
            NguoiDungs = db.GetTable<NGUOIDUNG>();

            var query = from nd in NguoiDungs.AsEnumerable()
                        where nd.TaiKhoan == taiKhoan
                        select nd;
            if (query.Any())
                return true;
            else
                return false;
        }
        /// <summary>
        /// Hàm kiểm tra đăng nhập có thành công hay không?
        /// </summary>
        /// <param name="taiKhoan"></param>
        /// <param name="matKhau"></param>
        /// <returns></returns>
        public bool KiemTraDangNhap(string taiKhoan, string matKhau)
        {
            NguoiDungs = db.GetTable<NGUOIDUNG>();

            var query = from nd in NguoiDungs.AsEnumerable()
                        where nd.TaiKhoan == taiKhoan && nd.MatKhau == matKhau
                        select nd;
            if (query.Any())
                return true;
            else
                return false;
        }

        public bool CapNhatNguoiDung(string taiKhoan, NGUOIDUNG ND)
        {
            NguoiDungs = db.GetTable<NGUOIDUNG>();
            NGUOIDUNG nd = NguoiDungs.Single(nd1 => nd1.TaiKhoan == taiKhoan);
            nd.TenNguoiDung = ND.TenNguoiDung;
            nd.MaVaiTro = ND.MaVaiTro;
            nd.NhomNguoiDung = nd.NhomNguoiDung;
            nd.DienGiai = ND.DienGiai;
            nd.TrangThai = ND.TrangThai;
            db.SubmitChanges();
            return true;
        }

        public bool KiemTraThuocNhomAdmin(string taiKhoan)
        {
            NguoiDungs = db.GetTable<NGUOIDUNG>();
            var query = from nd in NguoiDungs
                        where nd.TaiKhoan == taiKhoan && nd.NhomNguoiDung == "Admin"
                        select nd;
            if (query.Any())
                return true;
            else
                return false;
        }

        public bool KiemTraTaiKhoanPhanQuyen(string taiKhoan, string maVaiTro)
        {
            NguoiDungs = db.GetTable<NGUOIDUNG>();
            var query = from nd in NguoiDungs
                         where nd.MaVaiTro == maVaiTro && nd.TaiKhoan == taiKhoan
                         select nd;
            if (query.Any())
                return true;
            else
                return false;
        }

        public bool DoiMatKhau(string taiKhoan, string matKhauMoi)
        {
            NGUOIDUNG nd = NguoiDungs.Single(n => n.TaiKhoan == taiKhoan);
            
            try
            {
                nd.MatKhau = matKhauMoi;
                db.SubmitChanges();
                return true;
            }
            catch(Exception)
            {
                return false;
            }
        }

        public bool SuaNguoiDung(string ten, string taikhoan, string matkhau, 
            string mavaitro, string nhom, bool trangthai, string diengiai)
        {
            NguoiDungs = db.GetTable<NGUOIDUNG>();
            NGUOIDUNG nd = NguoiDungs.Single(n => n.TenNguoiDung == ten);

            try
            {
                nd.TaiKhoan = taikhoan;
                nd.MatKhau = matkhau;
                nd.MaVaiTro = mavaitro;
                nd.NhomNguoiDung = nhom;
                nd.TrangThai = trangthai;
                nd.DienGiai = diengiai;

                db.SubmitChanges();
                return true;
            }
            catch(Exception)
            { 
                return false; 
            }
        }

        public string LayMaTheoTen(string tenNguoiDung)
        {
            NguoiDungs = db.GetTable<NGUOIDUNG>();

            var query = (from nd in NguoiDungs
                         where nd.TenNguoiDung == tenNguoiDung
                         select nd.MaNguoiDung).FirstOrDefault();
            return query;
        }

        public Table<NGUOIDUNG> DanhSachNguoiDung()
        {
            return db.GetTable<NGUOIDUNG>();
        }
    }
}
